class HeapOperator {
    upAdjust(array = []) {
        let childIndex = array.length -1
        let parentIndex = (childIndex -1) / 2
        let temp = array[childIndex]
        while(childIndex > 0 && temp < array[parentIndex]) {
            array[childIndex] = array[parentIndex]
            childIndex = parentIndex
            parentIndex = (parentIndex - 1) / 2
        }
        array[childIndex] = temp
    }

    downAdjust(array = [],parentIndex, length) {
        let temp = array[parentIndex]
        let childIndex = 2 * parentIndex + 1
        while(childIndex < length) {
            if(childIndex + 1 < length && array[childIndex + 1] < array[childIndex]) {
                childIndex++
            }
            array[parentIndex] = array[childIndex]
            parentIndex = childIndex
            childIndex = 2 * childIndex + 1
        }
        array[parentIndex] = temp
    }

    buildHeap(array = []) {
        for (let i = (array.length - 2) / 2; i >= 0;i--) {
            this.downAdjust(array,i,array.length)
        }
    }
}

let array =  [1,3,2,6,5,7,8,9,10,0];
new HeapOperator().upAdjust(array)
console.log(array.toString())

array = [7,1,3,10,5,2,8,9,6]
new HeapOperator().buildHeap(array)
console.log(array.toString())
